﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using SuperMarket.DAL;

namespace SuperMarket
{
    public class SqlServerDBHelper:DBHelper
    {
        private const string DB_CONNECTION_STRING = @"Data Source=.\SQLEXPRESS;Initial Catalog=mmsdb;Integrated Security=True";
        private SqlConnection conn = new SqlConnection(DB_CONNECTION_STRING);


        public override void Open()
        {
            conn.Open();

        }

        public override void Close()
        {
            conn.Close();
        }


        public override int ExecuteNonQuery(string sql)
        {
            Open();
            int count = new SqlCommand(sql, conn).ExecuteNonQuery();
            Close();
            return count;
        }

        public override object ExecuteScalar(string sql)
        {
            Open();
            object data = new SqlCommand(sql, conn).ExecuteScalar();
            Close();
            return data;
        }

        public override System.Data.Common.DbDataReader ExecuteReader(string sql)
        {
            Open();
            return new SqlCommand(sql, conn).ExecuteReader();
        }

        public override DataTable ExecuteDadaList(string sql)
        {
            DataTable dt = new DataTable();//使用SqlDataAdapter查询并填充数据
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            da.Fill(dt);//填充数据
            return dt;
        }
    }
}
